import 'package:flutter/material.dart';
import 'package:x3_charts_core/chart_painter/abreast_histogram_chart_painter.dart';
import 'package:x3_charts_core/model/general_chart_model.dart';
import 'package:x3_charts_core/touch_fnfo/point_touch_info.dart';

///**
///* @author hrongyong
///* @date   3/1/21
///* @email hrongyong@gmail.com
///* Description:  并排直方图
///*/
class AbreastHistogramChartWidget extends StatefulWidget {
  final List<GeneralChartModel> chartList;

  ///左边Y轴最大值
  final int leftYAxisMax;

  ///左边Y轴的单位
  final String leftYAxisUnit;

  ///右边Y轴最大值
  final int rightYAxisMax;

  ///右边Y轴的单位
  final String rightYAxisUnit;

  ///背景色
  final Color backgroundColor;

  ///x轴线颜色
  final Color xAxisColor;

  ///x轴线描述颜色
  final Color xAxisDescriptionColor;

  ///y轴线颜色
  final Color yAxisColor;

  ///y轴线描述颜色
  final Color yAxisDescriptionColor;

  ///绘制Y轴线
  final bool drawYAxis;

  ///第一个直方的颜色
  final firstHistogramColor;

  ///第二根直方的颜色
  final secondHistogramColor;

  ///第一个折线的颜色
  final firstPolylineColor;

  ///第一个直方描述的前缀
  final String firstHistogramPrefix;

  ///第二个直方描述的前缀
  final String secondHistogramPrefix;

  ///第一个折线描述的前缀
  final String firstPolylinePrefix;

  ///是否显示描述
  final bool showDescription;

  ///是否点击点显示信息
  final bool clickTheDotsShowInfo;

  AbreastHistogramChartWidget({
    Key key,
    @required this.chartList,
    @required this.leftYAxisMax,
    @required this.rightYAxisMax,
    this.leftYAxisUnit = "",
    this.rightYAxisUnit = "",
    this.backgroundColor = Colors.white,
    this.xAxisColor = Colors.black12,
    this.yAxisColor = Colors.black12,
    this.xAxisDescriptionColor = Colors.black,
    this.yAxisDescriptionColor = Colors.black,
    this.drawYAxis = false,
    this.firstHistogramColor = const Color(0xFF727FC7),
    this.secondHistogramColor = const Color(0xFFC0C4DB),
    this.firstPolylineColor = const Color(0xFFD695AB),
    this.firstHistogramPrefix = "",
    this.secondHistogramPrefix = "",
    this.firstPolylinePrefix = "",
    this.showDescription = false,
    this.clickTheDotsShowInfo = true,
  });

  @override
  _AbreastHistogramChartWidgetState createState() => _AbreastHistogramChartWidgetState();
}

class _AbreastHistogramChartWidgetState extends State<AbreastHistogramChartWidget> with SingleTickerProviderStateMixin {
  final PointTouchInfo touchInfo = PointTouchInfo();

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onPanDown: _onPanDown,
      child: Container(
        child: LayoutBuilder(
          builder: (BuildContext context, BoxConstraints constraints) {
            return CustomPaint(
              size: Size(constraints.maxWidth, constraints.maxHeight),
              painter: AbreastHistogramChartPainter(
                chartList: widget.chartList,
                leftYAxisMax: widget.leftYAxisMax,
                leftYAxisUnit: widget.leftYAxisUnit,
                rightYAxisMax: widget.rightYAxisMax,
                rightYAxisUnit: widget.rightYAxisUnit,
                backgroundColor: widget.backgroundColor,
                xAxisColor: widget.xAxisColor,
                xAxisDescriptionColor: widget.xAxisDescriptionColor,
                yAxisColor: widget.yAxisColor,
                yAxisDescriptionColor: widget.yAxisDescriptionColor,
                drawYAxis: widget.drawYAxis,
                firstHistogramColor: widget.firstHistogramColor,
                secondHistogramColor: widget.secondHistogramColor,
                firstPolylineColor: widget.firstPolylineColor,
                pointTouchInfo: touchInfo,
                firstHistogramPrefix: widget.firstHistogramPrefix,
                secondHistogramPrefix: widget.secondHistogramPrefix,
                firstPolylinePrefix: widget.firstPolylinePrefix,
                showDescription: widget.showDescription,
                clickTheDotsShowInfo: widget.clickTheDotsShowInfo,
              ),
            );
          },
        ),
      ),
    );
  }

  ///点事件
  void _onPanDown(DragDownDetails details) {
    touchInfo.point = details.localPosition;
  }

  ///判断出是否在某点的半径为r圆范围内
  bool judgeCircleArea(Offset src, Offset dst, double r) => (src - dst).distance <= r;
}
